//
//  ShareMenuView.m
//  MemberProject
//
//  Created by mac on 15/5/20.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "ShareMenuView.h"
#import "ShareMenuViewCollectionViewCell.h"
@interface ShareMenuView()<UICollectionViewDataSource,UICollectionViewDelegate>
@property(strong,nonatomic)NSArray *data;
@property(strong,nonatomic)UIView *tagView;
@property(strong,nonatomic)UIButton *cancelButton;
@property(strong,nonatomic)UICollectionView *collectionView;
@end
@implementation ShareMenuView
-(instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        self.tagView=[[UIView alloc]initWithFrame:self.bounds];
        [self addSubview:self.tagView];
        self.tagView.backgroundColor=[UIColor blackColor];
        UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap:)];
        [self.tagView addGestureRecognizer:tap];
        
        UICollectionViewFlowLayout *layout=[[UICollectionViewFlowLayout alloc]init];
        layout.itemSize=CGSizeMake((self.width-20-10-2)/3, 70);
        layout.minimumInteritemSpacing=1;
        layout.minimumLineSpacing=1;
        UICollectionView *collectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(10, self.height, self.width-20, 70*2+2) collectionViewLayout:layout];
        [self addSubview:collectionView];
        self.collectionView=collectionView;
        collectionView.delegate=self;
        collectionView.dataSource=self;
        collectionView.backgroundColor=[UIColor whiteColor];
        collectionView.layer.cornerRadius=10.0f;
        [collectionView registerNib:[UINib nibWithNibName:@"ShareMenuViewCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"CellIdentifier"];
        self.data=@[@{@"image":[UIImage imageNamed:@"qq_icon"],@"text":@"QQ好友"},@{@"image":[UIImage imageNamed:@"qzone_icon"],@"text":@"QQ空间"},@{@"image":[UIImage imageNamed:@"wechat_icon"],@"text":@"微信好友"},@{@"image":[UIImage imageNamed:@"wechat_timeline_icon"],@"text":@"朋友圈"},@{@"image":[UIImage imageNamed:@"sms_icon"],@"text":@"短信"},@{@"image":[UIImage imageNamed:@"email_icon"],@"text":@"邮件"}];
        self.cancelButton=[UIButton buttonWithType:UIButtonTypeCustom];
        self.cancelButton.frame=CGRectMake(10, self.height+self.collectionView.height+5, self.width-20, 30);
        [self.cancelButton setTitle:@"取消" forState:UIControlStateNormal];
        [self.cancelButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
        [self.cancelButton addTarget:self action:@selector(chickCancelButton:) forControlEvents:UIControlEventTouchUpInside];
        self.cancelButton.layer.cornerRadius=10.0f;
        self.cancelButton.backgroundColor=[UIColor whiteColor];
        [self addSubview:self.cancelButton];
    }
    return self;
}

-(void)tap:(UITapGestureRecognizer *)sender{
    [self hide];
}

-(void)show{
    self.hidden=NO;
    self.tagView.alpha=0;
    [UIView animateWithDuration:0.5 animations:^{
        self.tagView.alpha=0.7;
        self.collectionView.y=self.height-self.collectionView.height-self.cancelButton.height-10;
        self.cancelButton.y=self.height-self.cancelButton.height-5;
    } completion:^(BOOL finished) {
        
    }];
}

-(void)hide{
    [UIView animateWithDuration:0.5 animations:^{
        self.tagView.alpha=0;
        self.collectionView.y=self.height;
        self.cancelButton.y=self.height+self.collectionView.height+5;
    } completion:^(BOOL finished) {
        self.hidden=YES;
    }];
}

-(void)chickCancelButton:(UIButton *)sender{
    [self hide];
}

#pragma mark - collectionView
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier=@"CellIdentifier";
    ShareMenuViewCollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    NSDictionary *d=self.data[indexPath.row];
    cell.imageView.image=d[@"image"];
    cell.label.text=d[@"text"];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    if([self.delegate respondsToSelector:@selector(shareMenuView:collectionView:didSelectItemAtIndexPath:)]){
        [self.delegate shareMenuView:self collectionView:self.collectionView didSelectItemAtIndexPath:indexPath];
    }
}
@end
